#ifndef __LOG_H__
#define __LOG_H__

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#define MAX_LOG_LINE_LEN 1024

using namespace std;
void ysip_log(const char *fmt, ...)
{
	char log[MAX_LOG_LINE_LEN], value[15];
	char c, *s, *q, *p = log;
	int d, i = MAX_LOG_LINE_LEN;

	va_list args;

	va_start(args, fmt);
	while(*fmt)
	{
		switch(*fmt++)
		{
			case 's':
				s = va_arg(args, char *);
				while(*s != '\0') 
				{
					*p++ = *s++;
					if(-- i <= 0) goto OUT;
				}
				strncpy(p, s, i);
				break;
			case 'd':
				d = va_arg(args, int);
				d = snprintf(value, 15, "%d", d);
				value[d] = '\0';
				q = value;
				while(*q != '\0')
				{
					*p++ = *q ++;
					if(--i <= 0) goto OUT;
				}
				break;
			case 'c':
				c = (char) va_arg(args, int);
				*p++ = c;
				if(--i <= 0) goto OUT;
				break;
			default:
				*p++ = *fmt;
				if(--i <=0) goto OUT;
		}
	}
OUT:
	;
	va_end(args);
	*p = '\0';
	fprintf(stderr, log, "");
}
#endif
